#!/bin/bash

# ===========================================
# 混淆配置测试脚本
# ===========================================

echo "🔍 测试混淆配置..."

# 检查必要文件是否存在
echo "检查配置文件..."

FILES=(
    "app/build.gradle"
    "app/proguard-rules.pro"
    "app/proguard-rules-debug.pro"
    "app/proguard-dictionary.txt"
)

for file in "${FILES[@]}"; do
    if [ -f "$file" ]; then
        echo "✅ $file 存在"
    else
        echo "❌ $file 不存在"
        exit 1
    fi
done

echo ""
echo "检查build.gradle配置..."

# 检查是否启用了混淆
if grep -q "minifyEnabled true" app/build.gradle; then
    echo "✅ 混淆已启用"
else
    echo "❌ 混淆未启用"
fi

# 检查是否启用了资源压缩
if grep -q "shrinkResources true" app/build.gradle; then
    echo "✅ 资源压缩已启用"
else
    echo "❌ 资源压缩未启用"
fi

# 检查debug模式是否也启用了混淆
if grep -A 5 "debug {" app/build.gradle | grep -q "minifyEnabled true"; then
    echo "✅ Debug模式混淆已启用"
else
    echo "❌ Debug模式混淆未启用"
fi

echo ""
echo "检查混淆规则文件..."

# 检查Release规则文件
if grep -q "MainActivity" app/proguard-rules.pro; then
    echo "✅ Release规则文件包含MainActivity保护"
else
    echo "❌ Release规则文件缺少MainActivity保护"
fi

# 检查Debug规则文件
if grep -q "MainActivity" app/proguard-rules-debug.pro; then
    echo "✅ Debug规则文件包含MainActivity保护"
else
    echo "❌ Debug规则文件缺少MainActivity保护"
fi

# 检查Debug规则是否保留了Log
if grep -q "# -assumenosideeffects class android.util.Log" app/proguard-rules-debug.pro; then
    echo "✅ Debug规则文件保留了Log调用"
else
    echo "❌ Debug规则文件可能移除了Log调用"
fi

echo ""
echo "检查混淆字典..."

# 检查字典文件大小
DICT_SIZE=$(wc -l < app/proguard-dictionary.txt)
if [ "$DICT_SIZE" -gt 100 ]; then
    echo "✅ 混淆字典包含 $DICT_SIZE 个条目"
else
    echo "❌ 混淆字典条目过少: $DICT_SIZE"
fi

echo ""
echo "🎯 混淆配置测试完成！"
echo ""
echo "下一步："
echo "1. 运行 ./build-release.sh debug 构建Debug混淆版本"
echo "2. 运行 ./build-release.sh 构建Release混淆版本"
echo "3. 测试混淆后的应用功能"
